// pages/workManage/peopleFeed.js
import {
	station,
	problem
} from '../../api/apiData'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		show: false,
		autosize: {
			minHeight: 100
		},
		atlas_id: '',
		info: '',
		fileList: [],
		fileList2: [],
		categoryList: [{
				name: '站点留言',
				id: 1
			},
			{
				name: '代表留言',
				id: 1
			}
		],
		category_name: '',
		category_id: '',
		staff_id: '',
		content: '',
		station_id: '',
		member_uid: '',
		contact_name:'',
		contact_phone:'',
		title:'',
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.setData({
			station_id: options.id
		})
		//  this.getCategory()


	},
	// 选择人员
	toChange() {
		let station_id = this.data.station_id
		wx.navigateTo({
			url: '/pages/contact/list?station_id=' + station_id,
		})
	},
	// 上传图片
	afterRead(event) {
		let that = this
		const {
			file
		} = event.detail;
		console.log(file)
		file.forEach(item => {
			wx.uploadFile({
				url: 'https://ncxbyx.ncscloudmedia.com/index.php/api/upload/file', // 仅为示例，非真实的接口地址
				filePath: item.path,
				name: 'file',
				formData: {
					user: 'test'
				},
				success(res) {
					console.log(res)
					const {
						fileList = [],
							fileList2 = []
					} = that.data;
					fileList.push({
						url: JSON.parse(res.data).data.url
					});
					fileList2.push(JSON.parse(res.data).data.path)
					console.log(fileList2)
					that.setData({
						fileList,
						fileList2,
					});
				},
			});
		})

	},
	deleteImg(event) {
		let index = event.detail.index
		let arr = this.data.fileList
		let arr2 = this.data.fileList2
		arr.splice(index, 1)
		arr2.splice(index, 1)
		this.setData({
			fileList: arr,
			fileList2: arr2
		})
	},
	onConfirm(e) {
		this.setData({
			category_id: e.detail.value.id,
			category_name: e.detail.value.name,
			show: false
		})
	},
	onCancel() {
		this.setData({
			show: false
		})
	},
	toShow() {
		this.setData({
			show: true
		})
	},
	// 提交
	toSubmit() {
		let obj = {
			type: 2,
			member_uid: this.data.member_uid,
			contact_name: this.data.contact_name,
			contact_phone: this.data.contact_phone,
			title: this.data.title,
			content: this.data.content,
			files: this.data.fileList2.toString(),
		}
		if (obj.member_id == '') {
			wx.showToast({
				title: '请选择代表',
				icon: 'error'
			})
			return false
		}
		wx.showModal({
			title: '提示',
			content: '你确定要提交吗？',
			showCancel: true, //是否显示取消按钮
			cancelText: "否", //默认是“取消”
			// cancelColor:'skyblue',//取消文字的颜色
			confirmText: "是", //默认是“确定”
			// confirmColor: 'skyblue',//确定文字的颜色
			success: function (res) {
				if (res.cancel) {
					//点击取消,默认隐藏弹框

				} else {
					//点击确定
					station.feedback.submit(obj, res => {
						if (res.code == 1) {
							wx.showToast({
								title: res.msg,
							})
							setTimeout(() => {
								wx.navigateTo({
									url: '/pages/suggest/my',
								})
							}, 500)
						} else {
							wx.showToast({
								title: res.msg,
								icon: 'error'
							})
						}
					})
				}
			},
			fail: function (res) {}, //接口调用失败的回调函数
			complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		console.log(this.data.atlas_id)
		if (this.data.atlas_id != '') {
			this.setData({
				member_uid: this.data.atlas_id
			})
			station.member.detail({
				member_uid: this.data.atlas_id
			}, res => {
				this.setData({
					info: res.data
				})
			})
		}

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})